﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace ImageTagEditor.ValueConverters
{
	/// <summary>
	/// Value converter for string where the case "" is converted to null and vice versa
	/// </summary>
	public class StringNullValueConverter : IValueConverter
	{

		/// <summary>
		/// Convert from a string where null may be used and convert it to "" if necessary.
		/// </summary>
		/// <param name="value">The value to convert</param>
		/// <param name="targetType">Not used</param>
		/// <param name="parameter">Not used</param>
		/// <param name="culture">Not used</param>
		/// <returns>The string converted to make sure null is ""</returns>
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (value == null)
				return "";
			else 
				return value;
		}

		/// <summary>
		/// Convert back from a string where "" may be used and convert it to null if necessary
		/// </summary>
		/// <param name="value">The value to convert</param>
		/// <param name="targetType">Not used</param>
		/// <param name="parameter">Not used</param>
		/// <param name="culture">Not used</param>
		/// <returns>The string converted back to make sure "" is null</returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (value == null)
				return value;
			else if (value is string)
			{
				if (value.ToString() == "")
					return null;
				else
					return value;
			}
			throw new Exception("Can only convert back from type of string");
		}

	}
}
